import { CellRange } from './cell_range';

export default class CustomClickCells {
  constructor() {
    this.refs = null;
  }

  addRef(ref) {
    if(this.refs === null) {
      this.refs = []
    }
    if(!this.refs.includes(ref)) this.refs.push(ref)
  }

  removeRef(ref) {
    if(this.refs === null) return
    if(this.refs.includes(ref)) {
      var index = this.refs.indexOf(ref)
      this.refs.splice(index, 1)
    }
  }

  setData({ refs }) {
    if (ref != null) {
      this.refs = refs;
    }
  }

  getData() {
    if (this.active()) {
      const { refs } = this;
      return { refs };
    }
    return {};
  }

  includes(ri, ci) {
    if (this.active()) {
      var ranges = this.range();
      for(var i=0;i<ranges.length;i++) {
        if(ranges[i].includes(ri, ci)) {
          return true
        }
      }
      return false
    }
    return false;
  }

  range() {
    return this.refs.map(((ref)=>{
      return CellRange.valueOf(ref);
    }))
  }

  clear() {
    this.refs = null;
  }

  active() {
    return this.refs !== null;
  }
}
